package me.littlesquid.bettertp;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.io.File;

public class guiclick implements Listener {
    @EventHandler
    public void onclick(InventoryClickEvent event) {
        File file=new File(me.littlesquid.bettertp.Bettertp.getProvidingPlugin(me.littlesquid.bettertp.Bettertp.class).getDataFolder(),"data.yml");
        FileConfiguration data= YamlConfiguration.loadConfiguration(file);
        Inventory inventory=event.getClickedInventory();
        String aname=event.getWhoClicked().getOpenInventory().getTitle();
        if(aname.equals("传送菜单")) {
            ItemStack itemStack = event.getCurrentItem();
            if (itemStack != null) {
                Material material = itemStack.getType();
                Player player1 = (Player) event.getWhoClicked();
                if (material == Material.PLAYER_HEAD) {
                    String name = itemStack.getItemMeta().getDisplayName();
                    Player player = Bukkit.getPlayer(name);
                    Location location = player.getLocation();
                    Location location1=player1.getLocation();
                    FileManager.savecurrentlocation(location1,data,player1.getName());
                    FileManager.reloadfile(data,file);
                    player1.teleport(location);
                }
                else if (material ==Material.BONE){
                    Location location1=player1.getLocation();
                    FileManager.savecurrentlocation(location1,data,player1.getName());
                    FileManager.reloadfile(data,file);
                    if (data.getString(player1.getName()+".death")!=null){
                        String loc= data.getString(player1.getName()+".death");
                        Location location=FileManager.loadlocation(loc);
                        player1.teleport(location);
                    }else{
                        player1.sendMessage(ChatColor.RED+"暂无死亡点!");
                        event.setCancelled(true);
                    }

                }
                else if(material==Material.RED_BED){
                    Location location1=player1.getLocation();
                    FileManager.savecurrentlocation(location1,data,player1.getName());
                    FileManager.reloadfile(data,file);
                    Location location=player1.getBedSpawnLocation();
                   player1.teleport(location);
                }
                else if(material==Material.ACACIA_SIGN){
                    Location location1=player1.getLocation();
                    if (data.getString(player1.getName()+".current")!=null){
                        String loc=data.getString(player1.getName()+".current");
                        Location location=FileManager.loadlocation(loc);
                        player1.teleport(location);
                    }else{
                        player1.sendMessage(ChatColor.RED+"暂无传送点!");
                        event.setCancelled(true);
                    }

                    FileManager.savecurrentlocation(location1,data,player1.getName());
                    FileManager.reloadfile(data,file);
                }
                else {
                    event.setCancelled(true);
                }
            }
        }
    }
}
